package cn.tedu.hotel.serve.controller;

import cn.tedu.hotel.serve.pojo.dto.HotelDTO;
import cn.tedu.hotel.serve.pojo.dto.HotelRoomInfoDTO;
import cn.tedu.hotel.serve.pojo.dto.HotelUpdateDTO;
import cn.tedu.hotel.serve.pojo.vo.*;
import cn.tedu.hotel.serve.service.IHotelService;
import cn.tedu.hotel.serve.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/hotels")
@Api(tags = "03. 酒店管理模块")
@Validated
public class HotelController {

    @Autowired
    IHotelService hotelService;

    @GetMapping("/user/getHotelListBySomethingOfLocation")
    @ApiOperation("前台酒店模糊查询")
    @ApiOperationSupport(order = 800)
    public JsonResult<List<HotelSearchVO>> getHotelListBySomethingOfLocation(String location){
        log.debug("开始处理【前台查询酒店列表】的请求");
        List<HotelSearchVO> hotelListBySomethingOfLocation = hotelService.getHotelListBySomethingOfLocation(location);
        return JsonResult.ok(hotelListBySomethingOfLocation);
    }

    @GetMapping("/user/getHotelListByContent")
    @ApiOperation("前台用户输入数据查询酒店")
    @ApiOperationSupport(order = 801)
    public JsonResult<List<HotelSearchVO>> getHotelListByContent(String content){
        log.debug("开始处理【前台用户输入数据查询酒店】的请求");
        List<HotelSearchVO> hotelListByContent = hotelService.getHotelListByContent(content);
        return JsonResult.ok(hotelListByContent);
    }

    @GetMapping("/user/{id:[0-9]+}/getByHotelLevelId")
    @ApiOperation("前台根据酒店id查询酒店的等级")
    @ApiOperationSupport(order = 802)
    public JsonResult<HotelLevelVO> getByHotelLevelId(@PathVariable Long id){
        log.debug("开始处理【前台根据酒店ID{}，查询酒店等级】的请求",id);
        HotelLevelVO hotelLevelVO = hotelService.getByHotelLevelId(id);
        return JsonResult.ok(hotelLevelVO);
    }

    @GetMapping("/user/{id:[0-9]+}/getHotelById")
    @ApiOperation("前台根据酒店ID查询酒店")
    @ApiOperationSupport(order = 803)
    @ApiImplicitParam(name = "id", value = "酒店ID", required = true, dataType = "long")
    public JsonResult<HotelStandardVO> getHotelById(
            @PathVariable @Range(min = 1, message = "请提交有效的ID值")Long id){
        log.debug("开始处理【前台根据酒店ID{}，查询酒店】的业务",id);
        HotelStandardVO hotelStandardVO = hotelService.getHotelById(id);
        return JsonResult.ok(hotelStandardVO);
    }

    @PostMapping("/admin/addHotelInfo")
    @ApiOperation("后台添加酒店")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addHotelInfo(@Validated HotelDTO hotelDTO){
        log.debug("开始处理【后台添加酒店】的请求,参数{}",hotelDTO);
        hotelService.addHotelInfo(hotelDTO);
        return JsonResult.ok();
    }

    @PostMapping("/admin/addAdminHotelRoomInfo")
    @ApiOperation("后台添加酒店房间信息")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 101)
    public JsonResult<Void> addAdminHotelRoomInfo(@Validated HotelRoomInfoDTO hotelRoomInfoDTO){
        log.debug("开始处理【后台添加酒店房间信息】的请求,参数{}",hotelRoomInfoDTO);
        hotelService.addAdminHotelRoomInfo(hotelRoomInfoDTO);
        return JsonResult.ok();
    }

    @PostMapping("/admin/{id:[0-9]+}/delete")
    @ApiOperation("后台根据ID删除酒店")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 200)
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "请提交有效的ID值") Long id) {
        log.debug("开始处理[根据ID后台删除酒店],参数:{}", id);
        hotelService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/admin/{id:[0-9]+}/deleteHotelRoomInfo")
    @ApiOperation("后台根据ID删除酒店房间数据")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 201)
    public JsonResult<Void> deleteHotelRoomInfo(
            @PathVariable @Range(min = 1, message = "请提交有效的ID值") Long id
    ){
        log.debug("开始处理后台[根据ID:{}删除酒店房间数据]", id);
        hotelService.deleteHotelRoomInfo(id);
        return JsonResult.ok();
    }

    @PostMapping("/admin/{id:[0-9]+}/update")
    @ApiOperation("后台修改酒店")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "主题相册ID", required = true, dataType = "long")
    public JsonResult<Void> update(@PathVariable @Range(min = 1, message = "请提交有效的ID值") Long id,
                                   @Validated HotelUpdateDTO hotelUpdateDTO) {
        log.debug("开始处理[根据ID后台修改酒店],参数:{},{}", id,hotelUpdateDTO);
        hotelService.update(id,hotelUpdateDTO);
        return JsonResult.ok();
    }

    @PostMapping("/admin/{id:[0-9]+}/updateAdminHotelRoomInfoById")
    @ApiOperation("后台根据酒店房间信息ID修改酒店房间信息")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 301)
    public JsonResult<Void> updateAdminHotelRoomInfoById(
            @PathVariable @Range(min = 1, message = "请提交有效的ID值") Long id,
            @Validated HotelRoomInfoDTO hotelRoomInfoDTO
            ){
        log.debug("开始处理【后台根据酒店房间信息ID：{}修改酒店房间信息】", id);
        hotelService.updateAdminHotelRoomInfoById(id,hotelRoomInfoDTO);
        return JsonResult.ok();
    }

    @GetMapping("/admin/getHotelList")
    @ApiOperation("后台查询酒店列表")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 400)
    public JsonResult<List<HotelVO>> getHotelList(){
        log.debug("开始处理【后台查询酒店列表】的请求");
        List<HotelVO> hotelList = hotelService.getHotelList();
        return JsonResult.ok(hotelList);
    }

    @GetMapping("/admin/getHotelLevelList")
    @ApiOperation("后台查询酒店星级列表")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 401)
    public JsonResult<List<HotelLevelVO>> getHotelLevelList(){
        log.debug("开始处理【后台查询酒店星级列表】的请求");
        List<HotelLevelVO> hotelLevelList = hotelService.getHotelLevelList();
        return JsonResult.ok(hotelLevelList);
    }

    @GetMapping("/admin/{id:[0-9]+}")
    @ApiOperation("后台查询酒店(单个)")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 402)
    public JsonResult<HotelStandardVO> getAdminHotelById(@PathVariable @Range(min = 1, message = "请提交有效的ID值")
                                                            Long id){
        log.debug("开始处理【后台查询酒店】的请求");
        HotelStandardVO hotelStandardVO = hotelService.getAdminHotelById(id);
        return JsonResult.ok(hotelStandardVO);
    }

    @GetMapping("/admin/getAdminHotelRoomInfoList")
    @ApiOperation("后台获取酒店房间信息列表")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 403)
    public JsonResult<List<HotelRoomInfoVO>> getAdminHotelRoomInfoList(){
        log.debug("开始处理【后台获取酒店房间信息列表】的请求");
        List<HotelRoomInfoVO> list = hotelService.getAdminHotelRoomInfoList();
        return JsonResult.ok(list);
    }

    @GetMapping("/admin/{id:[0-9]+}/getAdminHotelRoomInfoById")
    @ApiOperation("后台根据酒店ID查询酒店信息列表的数据")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 404)
    public JsonResult<HotelRoomInfoVO> getAdminHotelRoomInfoById(
            @PathVariable @Range(min = 1, message = "请提交有效的ID值") Long id
    ){
        log.debug("开始处理【后台根据酒店ID：{}查询酒店信息列表的数据】的请求",id);
        HotelRoomInfoVO hotelRoomInfoVO = hotelService.getAdminHotelRoomInfoById(id);
        return JsonResult.ok(hotelRoomInfoVO);
    }
}
